package sk.stuba.fiit.repka.dp.gui;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.lang.reflect.InvocationTargetException;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JWindow;
import javax.swing.SwingUtilities;

public class SplashWindow extends JWindow {
 
	public SplashWindow(int waitTime) {
		this.setAlwaysOnTop(true);
		ImageIcon image = new ImageIcon("." + File.separator + "images"
				+ File.separator + "splash.png");
		JLabel pictureLabel = new JLabel(image);
		this.add(pictureLabel);
		pack();
		
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension labelSize = pictureLabel.getPreferredSize();
		setLocation(screenSize.width / 2 - (labelSize.width / 2), screenSize.height / 2 - (labelSize.height / 2));
		
		addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				setVisible(false);
				dispose();
			}
		});
		
		final int pause = waitTime;
		
		final Runnable closeRunner = new Runnable() {
			
			@Override
			public void run() {
				setVisible(false);
				dispose();
			}
		};
		
		Runnable waitRunner = new Runnable() {
			
			@Override
			public void run() {
				try {
					Thread.sleep(pause);
					SwingUtilities.invokeAndWait(closeRunner);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		};
		
		setVisible(true);
		
		Thread splashThread = new Thread(waitRunner, "SplashThread");
		splashThread.start();
	}
}
